package com.usergood;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class UserGoodMapper extends Mapper<LongWritable, Text, UserGoodBean, IntWritable> {

    private final UserGoodBean keyOutUserGood = new UserGoodBean();
    private final IntWritable valueOutVisitNum = new IntWritable(1);

    /**
     * @param key
     * @param value   用户访问商品记录：U20230218183542473371	user1	G20230303104950942338	初恋50次	2023-12-03 09:24:20
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //U20230218183542473371	user1	G20230303104950942338	初恋50次	2023-12-03 09:24:20
        String strLine = value.toString();
        String[] strArray = strLine.split("\t");

        String strUserId = strArray[0];
        //String strUserName = strArray[1];
        String strGoodId = strArray[2];
        //String strGoodName = strArray[3];

        keyOutUserGood.setUserId(strUserId);
        //keyOutUserGood.setUserName(strUserName);
        keyOutUserGood.setGoodId(strGoodId);
        //keyOutUserGood.setGoodName(strGoodName);

        context.write(keyOutUserGood, valueOutVisitNum);
    }
}
